package com.xuyiwei.mayi.controller;

import com.xuyiwei.mayi.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by xuyiwei on 2019/4/21.
 */
@RestController
public class NginxController {
    @RequestMapping("/nginx")
    public String nginx(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return "欢迎来到nginx负载均衡8081";
    }


    @RequestMapping("/setSession")
    public String setSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        HttpSession session = request.getSession();
        User user = new User("你好","我不好");

        session.setAttribute("user",user);
        //然后往cook里面存一份

        return "设置session成功:8081";
    }
    @RequestMapping("/getSession")
    public String getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        HttpSession session = request.getSession(false);
//        if(session==null){
//            session = request.getSession();
//            Cookie[] cookies = request.getCookies();
//            //然后往里面读取 并且放入session里面
//        }
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        return "获得session成功:8081";
    }






}
